﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
	/// <summary>
	/// 数据访问类:GoodsInfo
	/// </summary>
	public partial class GoodsInfo
	{
        /// <summary>
        /// 所有上架商品信息
        /// </summary>
        public DataTable GetGoodsInfo(string DealerID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from v_GoodsInfo where 经销商编号='"+DealerID+"'");
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 所有上架商品信息
        /// </summary>
        public DataTable GetGoodsInfoByCache(string DealerID)
        {
            string CacheKey = "GetGoodsInfo-all" + DealerID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetGoodsInfo(DealerID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (DataTable)objModel;
        }

        /// <summary>
        /// 所有上架分类商品信息
        /// <param name="top">显示前N行，0为全部</param>
        /// <param name="type">是否推荐、是否折价、是否兑换</param>
        /// <param name="catagory">商品类别,0为全部</param>
        /// </summary>
        public DataTable GetGoodsInfo(int top, string type, int catagory, string DealerID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (top>0)
            {
                strSql.Append("top " + top.ToString()+" ");
            }
            strSql.Append("* from v_GoodsInfo where " + type + "=1 AND 经销商编号='" + DealerID + "'");
            if (catagory>0)
            {
                DataTable dt = new Category().GetChildsCategoryByCache(catagory.ToString());
                StringBuilder sb=new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append(dr["类别ID"]+",");
                }
                strSql.Append(" AND 类别ID in (" + sb.ToString().Remove(sb.ToString().Length - 1) + ")");
            }
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 所有上架分类商品信息
        /// <param name="top">显示前N行，0为全部</param>
        /// <param name="type">是否推荐、是否折价、是否兑换</param>
        /// <param name="catagory">商品类别,0为全部</param>
        /// </summary>
        public DataTable GetGoodsInfoByCache(int top, string type, int catagory, string DealerID)
        {
            string CacheKey = "GetGoodsInfo-" + top.ToString() + type + catagory.ToString() + DealerID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetGoodsInfo(top, type, catagory, DealerID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (DataTable)objModel;
        }

        /// <summary>
		/// 得到一个对象实体
		/// </summary>
        public QunYing.Model.GoodsInfo GetGoodsModel(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(
                "SELECT top 1 * FROM v_GoodsInfo");
            strSql.Append(" where 商品ID=@ID");
            SqlParameter[] parameters = {
                                            new SqlParameter("@ID", SqlDbType.Int, 4)
                                        };
            parameters[0].Value = id;

            QunYing.Model.GoodsInfo model = new QunYing.Model.GoodsInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["商品ID"] != null && ds.Tables[0].Rows[0]["商品ID"].ToString() != "")
                {
                    model.商品ID = int.Parse(ds.Tables[0].Rows[0]["商品ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["经销商编号"] != null && ds.Tables[0].Rows[0]["经销商编号"].ToString() != "")
                {
                    model.经销商编号 = ds.Tables[0].Rows[0]["经销商编号"].ToString();
                }
                if (ds.Tables[0].Rows[0]["条形码"] != null && ds.Tables[0].Rows[0]["条形码"].ToString() != "")
                {
                    model.条形码 = ds.Tables[0].Rows[0]["条形码"].ToString();
                }
                if (ds.Tables[0].Rows[0]["类别ID"] != null && ds.Tables[0].Rows[0]["类别ID"].ToString() != "")
                {
                    model.类别ID = int.Parse(ds.Tables[0].Rows[0]["类别ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["商品名称"] != null && ds.Tables[0].Rows[0]["商品名称"].ToString() != "")
                {
                    model.商品名称 = ds.Tables[0].Rows[0]["商品名称"].ToString();
                }
                if (ds.Tables[0].Rows[0]["商品描述"] != null && ds.Tables[0].Rows[0]["商品描述"].ToString() != "")
                {
                    model.商品描述 = ds.Tables[0].Rows[0]["商品描述"].ToString();
                }
                if (ds.Tables[0].Rows[0]["商品品牌"] != null && ds.Tables[0].Rows[0]["商品品牌"].ToString() != "")
                {
                    model.商品品牌 = ds.Tables[0].Rows[0]["商品品牌"].ToString();
                }
                if (ds.Tables[0].Rows[0]["重量"] != null && ds.Tables[0].Rows[0]["重量"].ToString() != "")
                {
                    model.重量 = decimal.Parse(ds.Tables[0].Rows[0]["重量"].ToString());
                }
                if (ds.Tables[0].Rows[0]["单位"] != null && ds.Tables[0].Rows[0]["单位"].ToString() != "")
                {
                    model.单位 = ds.Tables[0].Rows[0]["单位"].ToString();
                }
                if (ds.Tables[0].Rows[0]["原价"] != null && ds.Tables[0].Rows[0]["原价"].ToString() != "")
                {
                    model.原价 = decimal.Parse(ds.Tables[0].Rows[0]["原价"].ToString());
                }
                if (ds.Tables[0].Rows[0]["现价"] != null && ds.Tables[0].Rows[0]["现价"].ToString() != "")
                {
                    model.现价 = decimal.Parse(ds.Tables[0].Rows[0]["现价"].ToString());
                }
                if (ds.Tables[0].Rows[0]["赠送积分"] != null && ds.Tables[0].Rows[0]["赠送积分"].ToString() != "")
                {
                    model.赠送积分 = int.Parse(ds.Tables[0].Rows[0]["赠送积分"].ToString());
                }
                if (ds.Tables[0].Rows[0]["兑换积分"] != null && ds.Tables[0].Rows[0]["兑换积分"].ToString() != "")
                {
                    model.兑换积分 = int.Parse(ds.Tables[0].Rows[0]["兑换积分"].ToString());
                }
                if (ds.Tables[0].Rows[0]["是否推荐"] != null && ds.Tables[0].Rows[0]["是否推荐"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["是否推荐"].ToString() == "1") || (ds.Tables[0].Rows[0]["是否推荐"].ToString().ToLower() == "true"))
                    {
                        model.是否推荐 = true;
                    }
                    else
                    {
                        model.是否推荐 = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["是否折价"] != null && ds.Tables[0].Rows[0]["是否折价"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["是否折价"].ToString() == "1") || (ds.Tables[0].Rows[0]["是否折价"].ToString().ToLower() == "true"))
                    {
                        model.是否折价 = true;
                    }
                    else
                    {
                        model.是否折价 = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["是否兑换"] != null && ds.Tables[0].Rows[0]["是否兑换"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["是否兑换"].ToString() == "1") || (ds.Tables[0].Rows[0]["是否兑换"].ToString().ToLower() == "true"))
                    {
                        model.是否兑换 = true;
                    }
                    else
                    {
                        model.是否兑换 = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["添加日期"] != null && ds.Tables[0].Rows[0]["添加日期"].ToString() != "")
                {
                    model.添加日期 = DateTime.Parse(ds.Tables[0].Rows[0]["添加日期"].ToString());
                }
                if (ds.Tables[0].Rows[0]["更新日期"] != null && ds.Tables[0].Rows[0]["更新日期"].ToString() != "")
                {
                    model.更新日期 = DateTime.Parse(ds.Tables[0].Rows[0]["更新日期"].ToString());
                }
                if (ds.Tables[0].Rows[0]["商品图片"] != null && ds.Tables[0].Rows[0]["商品图片"].ToString() != "")
                {
                    model.DefaultImg=ds.Tables[0].Rows[0]["商品图片"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public QunYing.Model.GoodsInfo GetGoodsModelByCache(string id)
        {
            string CacheKey = "GetGoodsInfoModel-" + id.ToString();
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetGoodsModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (QunYing.Model.GoodsInfo)objModel;
        }

        /// <summary>
        /// 排行
        /// <param name="top">显示前N行，0为全部</param>
        /// </summary>
        public DataTable GetGoodsOrder(int top, string DealerID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (top > 0)
            {
                strSql.Append("top " + top.ToString() + " ");
            }
            strSql.Append(" ROW_NUMBER() OVER(order by 数量 DESC) AS 排行,* from V_GoodsOrder where  经销商编号='" + DealerID + "'");
            strSql.Append(" ORDER BY 数量 DESC");
            return DbHelperSQL.Query(strSql.ToString()).Tables[0];
        }

	}
}

